package cn.rui.service;

import cn.rui.mapper.PermMapper;
import cn.rui.pojo.Admins;
import cn.rui.pojo.Permissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 徽州大都督
 * @date 2020/8/13
 */
@Service
public class PermService {

    @Autowired
    private PermMapper permMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private AdminService adminService;

    /**
     * 查询所有管理员
     * @return
     */
    public List<Permissions> findAll() {


        List<Permissions> permissionsList = permMapper.selectList (null);

        for (Permissions permissions : permissionsList) {
            List<Admins> adminsList = adminService.findByPid (permissions.getPid ());
            String adminStr="";
            for (Admins admins : adminsList) {
                adminStr+=admins.getAdminUsername ()+",";
            }
            String substring = adminStr.substring (0, adminStr.lastIndexOf (","));


            permissions.setNamesStr (substring);
            permissions.setAdminsList (adminsList);
        }
        return permissionsList;
    }


    /**
     * 根据id查询
     * @param pid
     * @return
     */
    public Permissions findByPid(Integer pid) {
        Permissions permissions = permMapper.selectById (pid);

        List<Admins> adminsList = adminService.findByPid (pid);

        permissions.setAdminsList (adminsList);

        return permissions;
    }
}
